package cn.itcast.service.cargo.impl;

import cn.hutool.core.util.IdUtil;
import cn.itcast.dao.cargo.ExportDao;
import cn.itcast.dao.cargo.PackDao;
import cn.itcast.domain.cargo.Export;
import cn.itcast.domain.cargo.ExportExample;
import cn.itcast.domain.cargo.Pack;
import cn.itcast.domain.cargo.PackExample;
import cn.itcast.service.cargo.PackService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.List;


@Service
public class PackServiceImpl implements PackService {

    @Autowired
    private ExportDao exportDao;

    @Autowired
    private PackDao packDao;


    @Override
    public void save(Pack pack) {
        pack.setPackingListId(IdUtil.simpleUUID());
        String[] packArray = pack.getExportIds().split(",");
        ExportExample exportExample = new ExportExample();
        ExportExample.Criteria criteria = exportExample.createCriteria();
        criteria.andIdIn(Arrays.asList(packArray));
        List<Export> exportList = exportDao.selectByExample(exportExample);
       Integer exportNos = 0;
        for (Export export : exportList) {
            exportNos += export.getProNum();
        }
        pack.setState(1L);
        pack.setExportNos(exportNos.toString());
        packDao.insertSelective(pack);
    }

    @Override
    public void delete(String id) {
        packDao.deleteByPrimaryKey(id);
    }

    @Override
    public Pack findById(String id) {
        return packDao.selectByPrimaryKey(id);
    }

    @Override
    public void update(Pack pack) {
        packDao.updateByPrimaryKeySelective(pack);

    }

    @Override
    public List<Pack> findAll(PackExample packExample) {
        return null;
    }

    @Override
    public List<Pack> findAll() {
        return packDao.findAll();
    }

    @Override
    public PageInfo findByPage(int pageNum, int pageSize, PackExample example) {
        PageHelper.startPage(pageNum, pageSize);
        List<Pack> list = packDao.selectByExample(example);
        return new PageInfo(list);
    }
}
